/**
 * @filename:UsersController 2020-07-01 11:26:46
 * @project com/lwl/blue  V1.0
 * Copyright(c) 2020 <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a> Co. Ltd.
 * All right reserved.
 */
package com.lwl.blue.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwl.blue.commont.base.*;
import com.lwl.blue.commont.enums.RoleTypeEnum;
import com.lwl.blue.commont.utils.MD5Utils;
import com.lwl.blue.commont.utils.UserContextUtil;
import com.lwl.blue.entity.Sample;
import com.lwl.blue.entity.Users;
import com.lwl.blue.service.UsersService;
import com.lwl.blue.vo.LoginUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>自定义方法写在这里</p>
 *
 * <p>说明： 用户表API接口层</P>
 *
 * @version: V1.0
 * @author: <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a>
 */
@RestController
@RequestMapping("/users")
@Slf4j
public class UsersController extends BaseController {
    @Autowired
    UsersService usersService;

    @PostMapping("/pages")
    public PageResult<Users> getPages(@RequestBody PageParam<Users> pageParam) {
        JsonResult<Page<Users>> pages = usersService.pages(pageParam);
        PageResult<Users> re = new PageResult<>();
        re.success(pages.getData().getTotal(), pages.getData().getRecords());
        return re;
    }

    @PostMapping("power")
    public JsonResult power(@RequestBody Users users) {
        if (RoleTypeEnum.MAIN.getValue() < getUserInfo().getRoleType()) {
            return new JsonResult().error("没有操作权限");
        }
        Users update = usersService.getById(users.getId());
        update.setId(users.getId());
        update.setPowerLastTime(users.getPowerLastTime());
        update.setPowerDataType(users.getPowerDataType());
        setInsetOrUpdateUser(update);
        usersService.updateById(update);
        return new JsonResult().success("请求成功");
    }

    @PostMapping("updatePwd")
    public JsonResult updatePwd(@RequestBody Users users) {
        if (RoleTypeEnum.ALL.getValue() != getUserInfo().getRoleType()) {
            return new JsonResult().error("没有操作权限");
        }
        Users update = usersService.getById(users.getId());
        update.setId(users.getId());
        String pwd;
        try {
            pwd = MD5Utils.EncoderByMd5(users.getPassword());
        } catch (Exception e) {
            return new JsonResult().error("密码加密失败");
        }
        update.setPassword(pwd);
        setInsetOrUpdateUser(update);
        usersService.updateById(update);
        return new JsonResult().success("请求成功");
    }

    @PostMapping("saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody Users users) {
        if (RoleTypeEnum.ALL.getValue() != getUserInfo().getRoleType()) {
            return new JsonResult().error("没有操作权限");
        }
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Users::getUserName, users.getUserName());
        if (null != users.getId()) {
            wrapper.lambda().notIn(Users::getId, users.getId());
        }
        int count = usersService.count(wrapper);
        if (count > 0) {
            throw new BizException(ErrorCode.USER_EXISTS_ERROR.getCode(), ErrorCode.USER_EXISTS_ERROR.getMsg());
        }
        setInsetOrUpdateUser(users);
        users.setPassword(null);
        String pwd;
        if (Objects.isNull(users.getId())) {
            users.setPassword("123456");
            try {
                pwd = MD5Utils.EncoderByMd5(users.getPassword());
            } catch (Exception e) {
                return new JsonResult().error("密码加密失败");
            }
            users.setPassword(pwd);
        }

        usersService.saveOrUpdate(users);
        return new JsonResult().success("请求成功");
    }

    @PostMapping("/delete/{id}")
    public JsonResult saveOrUpdate(@PathVariable(value = "id") Integer id) {
        if (RoleTypeEnum.ALL.getValue() != getUserInfo().getRoleType()) {
            return new JsonResult().error("没有操作权限");
        }
        Users users = usersService.getById(id);
        if ("admin".equals(users.getUserName())) {
            return new JsonResult().error("超级管理员账号无法删除");
        }
        usersService.removeById(id);
        return new JsonResult().success("请求成功");
    }
}
